<?php
class RequestItemsController extends AppController {

    var $name = 'RequestItems';

    function admin_index() {
        $this->RequestItem->recursive = 0;
        $this->set('requestItems', $this->paginate());
    }

    function admin_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid request item', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('requestItem', $this->RequestItem->read(null, $id));
    }

    function admin_add() {
        if (!empty($this->data)) {
            $this->RequestItem->create();
            if ($this->RequestItem->save($this->data)) {
                $this->Session->setFlash(__('The request item has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The request item could not be saved. Please, try again.', true));
            }
        }
        $requests = $this->RequestItem->Request->find('list');
        $categories = $this->RequestItem->Category->find('list');
        $subCategories = $this->RequestItem->SubCategory->find('list');
        $this->set(compact('requests', 'categories', 'subCategories'));
    }

    function admin_edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid request item', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->RequestItem->save($this->data)) {
                $this->Session->setFlash(__('The request item has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The request item could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->RequestItem->read(null, $id);
        }
        $requests = $this->RequestItem->Request->find('list');
        $categories = $this->RequestItem->Category->find('list');
        $subCategories = $this->RequestItem->SubCategory->find('list');
        $this->set(compact('requests', 'categories', 'subCategories'));
    }

    function admin_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for request item', true));
            $this->redirect(array('action'=>'index'));
        }
        if ($this->RequestItem->delete($id)) {
            $this->Session->setFlash(__('Request item deleted', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Session->setFlash(__('Request item was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }
}
?>